package com.tuliyou.controller;

import com.tuliyou.common.util.Result;
import com.tuliyou.request.LoginRequest;
import com.tuliyou.request.RegisterRequest;
import com.tuliyou.request.UpdateCurrentRequest;
import com.tuliyou.dto.UserDTO;
import com.tuliyou.service.UserService;
import com.tuliyou.vo.LoginVO;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

// UserController.java
@RestController
@RequestMapping("/api/users")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/register")
    public Result<UserDTO> register(@Valid @RequestBody RegisterRequest request) {
        log.info("用户注册请求：username={}", request.getUsername());
        return userService.register(request);
    }

    @PostMapping("/login")
    public Result<LoginVO> login(@Valid @RequestBody LoginRequest request) {
        log.info("用户登录请求：username={}", request.getUsername());
        return userService.login(request);
    }

    @GetMapping("/current")
    public Result<UserDTO> getCurrentUser() {
        log.info("获取当前用户信息请求");
        return userService.getCurrentUser();
    }

    @PutMapping("/current")
    public Result<UserDTO> updateCurrentUser(@Valid @RequestBody UpdateCurrentRequest request) {
        log.info("更新当前用户信息请求");
        return userService.updateCurrentUser(request);
    }

    @PostMapping("logoff")
    public Result logoff() {
        log.info("注销当前用户");
        return userService.logoff();
    }
}